<?php
namespace Goatherd\Content\Engine\Reader\Buffer;

use Goatherd\Content\Engine\Resource\IResource;

use Goatherd\Content\Engine\Queue\IItem;

use Goatherd\Content\Engine\Resource\ResourceCollection;

use Goatherd\Commons\Collection\ICollection;

/**
 * Collection/ Mockup mapper.
 *
 * Stores resources for session only.
 *
 * @category ContentEngine
 * @package Goatherd\Content\Engine\Reader
 * @subpackage Buffer
 */
class Collection
extends Blackhole
{
    /**
     *
     * @var \Goatherd\Commons\Collection\ICollection
     */
    protected $_collection = null;

    /**
     *
     */
    public function __construct()
    {
        $this->_collection === null
          and $this->_collection = new ResourceCollection();
    }

    /**
     *
     * @param ICollection $coll
     */
    public function setCollection(ICollection $coll)
    {
        $this->_collection = $coll;
    }

    /**
     *
     * @return ICollection
     */
    public function getCollection()
    {
        return $this->_collection;
    }

    /**
     * (non-PHPdoc)
     * @see Goatherd\Content\Engine\Reader\Buffer.Blackhole::hasResource()
     */
    public function hasResource(IItem $item)
    {
        $id = $this->getResourceIdentifier($item);
        return $this->_collection->offsetExists($id);
    }

    /**
     * (non-PHPdoc)
     * @see Goatherd\Content\Engine\Reader\Buffer.Blackhole::getResource()
     */
    public function getResource(IItem $item)
    {
        if ($this->hasResource($item)) {
            $id = $this->getResourceIdentifier($item);
            return $this->_collection->offsetGet($id);
        }
    }

    /**
     * (non-PHPdoc)
     * @see Goatherd\Content\Engine\Reader\Buffer.Blackhole::setResource()
     */
    public function setResource(IItem $item, IResource $data)
    {
        $id = $this->getResourceIdentifier($item);
        return $this->_collection->offsetSet($id, $data);
    }
}